package mini.statement;

import java.util.List;
import java.util.LinkedList;

import mini.Env;
import mini.StatementList;
import mini.expr.Element;
import mini.expr.Expr;


public class WhileStatement extends Statement {

    private Expr expr;
    private StatementList stmtlist;

    public WhileStatement(Expr e, StatementList list) {
        expr = e;
        stmtlist = list;
    }

    public void eval(List<Env> listOfEnvs, LinkedList var) throws Exception {
        Element eval = expr.eval(listOfEnvs, var);
        
        if (!eval.isInt()) {
        	throw new IllegalArgumentException("Parse error. Expression does not evaluate to an int.");
        }
        
		while (expr.eval(listOfEnvs, var).getIntValue() > 0) {
            stmtlist.eval(listOfEnvs, var);
        }
    }
}